package com.castle.fortress.admin.goods.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.goods.entity.GoodsCategoryEntity;
import com.castle.fortress.admin.goods.dto.GoodsCategoryDto;
import com.castle.fortress.admin.goods.mapper.GoodsCategoryMapper;
import com.castle.fortress.admin.goods.service.GoodsCategoryService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 商品分类表 服务实现类
 *
 * @author majunjie
 * @since 2021-11-22
 */
@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategoryEntity> implements GoodsCategoryService {
    /**
    * 获取查询条件
    * @param goodsCategoryDto
    * @return
    */
    public QueryWrapper<GoodsCategoryEntity> getWrapper(GoodsCategoryDto goodsCategoryDto){
        QueryWrapper<GoodsCategoryEntity> wrapper= new QueryWrapper();
        if(goodsCategoryDto != null){
            GoodsCategoryEntity goodsCategoryEntity = ConvertUtil.transformObj(goodsCategoryDto,GoodsCategoryEntity.class);
            wrapper.like(StrUtil.isNotEmpty(goodsCategoryEntity.getName()),"name",goodsCategoryEntity.getName());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<GoodsCategoryDto> pageGoodsCategory(Page<GoodsCategoryDto> page, GoodsCategoryDto goodsCategoryDto) {
        QueryWrapper<GoodsCategoryEntity> wrapper = getWrapper(goodsCategoryDto);
        Page<GoodsCategoryEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<GoodsCategoryEntity> goodsCategoryPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<GoodsCategoryDto> pageDto = new Page(goodsCategoryPage.getCurrent(), goodsCategoryPage.getSize(),goodsCategoryPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(goodsCategoryPage.getRecords(),GoodsCategoryDto.class));
        return pageDto;
    }

    @Override
    public IPage<GoodsCategoryDto> pageGoodsCategoryExtends(Page<GoodsCategoryDto> page, GoodsCategoryDto goodsCategoryDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        GoodsCategoryEntity goodsCategoryEntity = ConvertUtil.transformObj(goodsCategoryDto,GoodsCategoryEntity.class);
        List<GoodsCategoryEntity> goodsCategoryList=baseMapper.extendsList(pageMap,goodsCategoryEntity);
        Long total = baseMapper.extendsCount(goodsCategoryEntity);
        Page<GoodsCategoryDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(goodsCategoryList,GoodsCategoryDto.class));
        return pageDto;
    }
    @Override
    public GoodsCategoryDto getByIdExtends(Long id){
        GoodsCategoryEntity  goodsCategoryEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(goodsCategoryEntity,GoodsCategoryDto.class);
    }

    @Override
    public List<GoodsCategoryDto> listGoodsCategory(GoodsCategoryDto goodsCategoryDto){
        QueryWrapper<GoodsCategoryEntity> wrapper = getWrapper(goodsCategoryDto);
        List<GoodsCategoryEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,GoodsCategoryDto.class);
    }

    @Override
    public List<GoodsCategoryDto> treeList() {
        QueryWrapper<GoodsCategoryEntity> wrapper= new QueryWrapper();
        wrapper.orderByDesc("sort");
        List<GoodsCategoryDto> list = ConvertUtil.transformObjList(baseMapper.selectList(wrapper) , GoodsCategoryDto.class);
        return ConvertUtil.listToTree(list);
    }

    @Override
    public List<GoodsCategoryDto> childrenListById(Long id) {
        if(id == null){
            return new ArrayList<>();
        }
        QueryWrapper<GoodsCategoryEntity> wrapper= new QueryWrapper();
        wrapper.eq("parent_id" , id);
        wrapper.orderByDesc("sort");
        List<GoodsCategoryDto> list = ConvertUtil.transformObjList(baseMapper.selectList(wrapper) , GoodsCategoryDto.class);
        return list;
    }

    @Override
    public void deletePidById(Long id) {
        baseMapper.deletePidById(id);
    }

    @Override
    public List<GoodsCategoryDto> allChildrenListById(Long id) {
        List<GoodsCategoryDto> list = ConvertUtil.transformObjList(list(),GoodsCategoryDto.class);
        Map<String, GoodsCategoryDto> map = new HashMap<>();
        Map<String, List<GoodsCategoryDto>> pMap = new HashMap<>();
        for (GoodsCategoryDto dto : list) {
            map.put("" + dto.getId(), dto);
            if(dto.getParentId() != null){
                String pid = dto.getParentId() + "";
                if(pMap.get(pid) == null){
                    List<GoodsCategoryDto> list1 = new ArrayList<>();
                    list1.add(dto);
                    pMap.put(pid , list1);
                }else {
                    pMap.get(pid).add(dto);
                }
            }
        }
        List<GoodsCategoryDto> returnList = new ArrayList<>();
        test(pMap ,returnList,id);
        return returnList;
    }

    private void test (Map<String, List<GoodsCategoryDto>> map , List<GoodsCategoryDto> list , Long id){
        List<GoodsCategoryDto> testList = map.get(id+"");
        if(testList == null || testList.size() < 1){
            return;
        }else {
            list.addAll(testList);
            for(GoodsCategoryDto dto : testList){
                test(map , list , dto.getId());
            }
        }
    }
}

